package com.mustafaiev.tair.cts.tapestry.pages.payers;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.mustafaiev.tair.cts.facade.IPayerFacade;
import com.mustafaiev.tair.cts.factory.ICostTrackingSystemFacadeFactory;
import com.mustafaiev.tair.cts.tapestry.dto.PayerDTO;

public class Add {

    @Inject
    private ICostTrackingSystemFacadeFactory facadeFactory;

    @InjectPage
    private Index payersPage;

    @Property
    @Persist
    private PayerDTO payer;

    @Component(id = "addPayerForm")
    private Form form;

    @SetupRender
    void setupRender() {
        payer = new PayerDTO();
    }

    @CommitAfter
    Object onSuccessFromAddPayerForm() {
        getFacade().savePayer(payer);
        return payersPage;
    }

    @OnEvent(value = "back")
    Object goBack() {
        return payersPage;
    }

    private IPayerFacade getFacade() {
        final IPayerFacade facade = facadeFactory
                .getFacade(IPayerFacade.class);
        return facade;
    }

}
